package application;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import game.Game;

/**
 * This class is used for serialization of class Game
 * (runtime saving and loading of the game).
 * 
 * @author Tomas Zima
 */
public class Serialization
{
	/**
	 * Serializes running game and save it to the defined file.
	 * 
	 * @param runningGame
	 * @param filePath
	 */
	public static void serialize(Game runningGame, String filePath)
	{
		try
		{
			FileOutputStream fileOutputStream		= new FileOutputStream(filePath);
			ObjectOutputStream objectOutputStream	= new ObjectOutputStream(fileOutputStream);
			
			objectOutputStream.writeObject(runningGame);
			objectOutputStream.close();
			fileOutputStream.close();
		}
		catch (IOException errHandler)
		{
			//
		}
	}
	
	/**
	 * Deserializes game from file and return reference to object.
	 * 
	 * @param filePath
	 * @return
	 */
	public static Game deserialize(String filePath)
	{
		Game result = null;
		
		try
		{
			FileInputStream fileInputStream		= new FileInputStream(filePath);
			ObjectInputStream objectInputStream	= new ObjectInputStream(fileInputStream);
			
			result = (Game) objectInputStream.readObject();
			
			objectInputStream.close();
			fileInputStream.close();
		}
		catch (IOException errHandler)
		{
			//
		}
		catch (ClassNotFoundException errHandler)
		{
			//
		}
		
		return result;
	}
}
